﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using NTQOnlineExam.Services.EntityFW.Entity;
using NTQOnlineExam.Services.Models;
using NTQOnlineExam.Services.ReportService.Account;

namespace NTQOnlineExam.Services.CommandService.Payment
{
    public class OrderPaymentInfoFactory : IOrderPaymentInfoFactory
    {
        private readonly IAccountReportService _accountReportService;

        public OrderPaymentInfoFactory(IAccountReportService accountReportService)
        {
            _accountReportService = accountReportService;
        }

        public OrderPaymentDTO Create(OrderDTO orderDto)
        {
            var user = _accountReportService.GetByUserId(orderDto.UserId);
            var orderPayment = new OrderPaymentDTO
            {
                OrderId = orderDto.OrderId,
                SubTotal = orderDto.SubTotal,
                Firstname = !string.IsNullOrWhiteSpace(orderDto.FirstName) ? orderDto.FirstName : (user != null ? user.FirstName : string.Empty),
                Lastname = !string.IsNullOrWhiteSpace(orderDto.LastName) ? orderDto.LastName : (user != null ? user.LastName : string.Empty),
                Street = orderDto.BillingAddress,
                City = orderDto.BillingCity,
                ZipCode = orderDto.BillingZipCode,
                PaymentFee = orderDto.PaymentFee ?? 0,
                Phone = (user != null ? user.Phone : string.Empty),

                SuccessUrl = "/Basket/OrderConfirmation",
                CancelUrl = "/Basket/Checkout",
                Email = orderDto.Email
            };
            if (orderDto.OrderDetails != null)
            {
                orderPayment.Items = new List<OrderPaymentDetailDTO>();
                foreach (var detail in orderDto.OrderDetails)
                {
                    orderPayment.Items.Add(new OrderPaymentDetailDTO()
                    {
                        ItemId = detail.Id,
                        Name = detail.TermName,
                        Price = detail.Price ?? 0,
                        Quantity = 1
                    });
                }
            }
            return orderPayment;
        }
    }
}
